package com.sdhs.file.utils;

import com.sdhs.common.utils.AESUtils;
import com.sdhs.common.utils.spring.SpringUtils;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.myproject.domain.GcMyprojectProfitMarginMeasurement;
import com.sdhs.myproject.service.IGcMyprojectProfitMarginMeasurementService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class ManageListUtil {

    private static final IGcMyprojectProfitMarginMeasurementService gcProfitMarginMeasurementService = SpringUtils.getBean(IGcMyprojectProfitMarginMeasurementService.class);


    public static void UpdateMeasurement(Long projectId, List<GcManageList> gcManageList) {
        // 初始化三个累加器
        BigDecimal sumContract = BigDecimal.ZERO;       // (合同数量 × 含税单价)的和
        BigDecimal sumCost = BigDecimal.ZERO;          // ((设备+施工+服务) × 合同数量)的和
        BigDecimal totalRatio = BigDecimal.ZERO;       // 最终比率
        if (!gcManageList.isEmpty() && projectId != null) {

            for(GcManageList item:gcManageList){
                // 获取各项值（String类型转换为BigDecimal）
                BigDecimal quantity = AESUtils.safeConvertToBigDecimal(item.getQuantity());
                BigDecimal salePrice = AESUtils.safeConvertToBigDecimal(item.getSalePrice());
                BigDecimal devicePrice = AESUtils.safeConvertToBigDecimal(item.getDevicePrice());
                BigDecimal constructionPrice = AESUtils.safeConvertToBigDecimal(item.getConstructionPrice());
                BigDecimal otherPrice = AESUtils.safeConvertToBigDecimal(item.getOtherPrice());

                // 计算 (合同数量 × 含税单价)
                BigDecimal contractItem = quantity.multiply(salePrice);
                sumContract = sumContract.add(contractItem);

                // 计算 (设备+施工+服务) × 合同数量
                BigDecimal costItem = (devicePrice.add(constructionPrice).add(otherPrice)).multiply(quantity);
                sumCost = sumCost.add(costItem);
            }
        }
        /*根据项目id查询我的项目-利润率测算-管理成本上限*/
        GcMyprojectProfitMarginMeasurement projectProfitMarginMeasurement = gcProfitMarginMeasurementService.selectGcMyprojectProfitMarginMeasurementById(projectId);
        BigDecimal overheadCostCap = projectProfitMarginMeasurement.getOverheadCostCap(); //  管理成本上限
        // 计算最终比率（防止除以零）
        if(sumContract.compareTo(BigDecimal.ZERO) != 0){
            /*计算最终比率{(合同数量 × 含税单价)-(设备+施工+服务) × 合同数量-管理成本上限}/(合同数量 × 含税单价)*/
            BigDecimal rawRatio = (sumContract.subtract(sumCost).subtract(overheadCostCap)).divide(sumContract, 10, RoundingMode.HALF_UP);
            // 乘以100并保留2位小数
            totalRatio = rawRatio.multiply(new BigDecimal("100"))
                    .setScale(2, RoundingMode.HALF_UP);
        }
        /*推送过来的数据计算后修改我的项目-利润率测算-标前利润率*/
        GcMyprojectProfitMarginMeasurement gcMyprojectProfitMarginMeasurement = new GcMyprojectProfitMarginMeasurement();
        gcMyprojectProfitMarginMeasurement.setProjectId(projectId);
        gcMyprojectProfitMarginMeasurement.setPreBidUtilization(String.valueOf(totalRatio));
        gcProfitMarginMeasurementService.updateMeasurement(gcMyprojectProfitMarginMeasurement);

    }
}
